package net.zhengxinyun.performance.controller.indicators;

import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.entity.CountryIndicatorsEntity;
import net.zhengxinyun.performance.entity.CountryScoreEntity;
import net.zhengxinyun.performance.entity.TokenEntity;
import net.zhengxinyun.performance.entity.UserEntity;
import net.zhengxinyun.performance.filter.CountryIndicatorsFilter;
import net.zhengxinyun.performance.filter.CountryScoreFilter;
import net.zhengxinyun.performance.service.indicators.CountryIndicatorsService;
import net.zhengxinyun.performance.service.indicators.CountryScoreService;
import net.zhengxinyun.performance.service.login.LoginService;
import net.zhengxinyun.performance.service.login.TokenService;
import net.zhengxinyun.performance.service.login.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 */
@RestController
@RequestMapping("/countryScore")
public class CountryScoreController {
    private CountryIndicatorsService countryIndicatorsService;

    private TokenService tokenService;

    private CountryScoreService countryScoreService;

    private UserService userService;

    private LoginService loginService;


    @Autowired
    public CountryScoreController(CountryIndicatorsService countryIndicatorsService, TokenService tokenService, CountryScoreService countryScoreService, UserService userService, LoginService loginService) {
        this.countryIndicatorsService = countryIndicatorsService;
        this.tokenService = tokenService;
        this.countryScoreService = countryScoreService;
        this.userService = userService;
        this.loginService = loginService;
    }

    @PostMapping("/pass")
    public Result<String> pass(@RequestBody CountryScoreParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            int i;
            TokenEntity tokenEntity = tokenService.queryByValue(param.tokenEntity.getValue());
            UserEntity userEntity = userService.queryById(tokenEntity.getUserId());
            //判断角色权限
            if ("admin".equals(userEntity.getUserPermissions())) {
                //判断是单个审核通过还是批量审核通过
                if (param.countryScoreEntity != null) {
                    //判断审核状态，只有在责任人审核通过后才能进行审核
                    if (!"1".equals(param.countryScoreEntity.getAudit())) {
                        return ResultUtils.getResults(Result.Status.ERROR, "还未到管理员审核阶段,请稍等");
                    }
                  //  param.countryScoreEntity.setId(Long.valueOf(param.countryScoreEntity.getAlternateField2()));
                   // param.countryScoreEntity.setAlternateField2(null);
                    i = countryScoreService.adminPass(param.countryScoreEntity);
                    if (i == 0) {
                        return ResultUtils.getResults(Result.Status.ERROR, "操作失败，请联系管理员");
                    }
                    String str = countryScoreService.setFirstFinalScore(param.countryScoreEntity);
                    if ("一级指标得分设置失败".equals(str)) {
                        return ResultUtils.getResults(Result.Status.ERROR, str);
                    }
                }
                if (param.list != null) {
                    String str = countryScoreService.adminAllPass(param.list);
                    if (!"操作成功".equals(str)) {
                        return ResultUtils.getResults(Result.Status.ERROR, str);
                    }
                    for (CountryScoreEntity c : param.list) {
                        String s = countryScoreService.setFirstFinalScore(c);
                        if ("一级指标得分设置失败".equals(s)) {
                            return ResultUtils.getResults(Result.Status.ERROR, str);
                        }
                    }
                }
            } else if ("responsible".equals(userEntity.getUserPermissions())) {
                if (param.countryScoreEntity != null) {
                    if (!"0".equals(param.countryScoreEntity.getAudit())) {
                        return ResultUtils.getResults(Result.Status.ERROR, "还未到责任人审核阶段,请稍等");
                    }
                   // param.countryScoreEntity.setId(Long.valueOf(param.countryScoreEntity.getAlternateField2()));
                   // param.countryScoreEntity.setAlternateField2(null);
                    i = countryScoreService.responsiblePass(param.countryScoreEntity);
                    if (i == 0) {
                        return ResultUtils.getResults(Result.Status.ERROR, "操作失败，请联系管理员");
                    }
                }
                if (param.list != null) {
                    String str = countryScoreService.responsibleAllPass(param.list).toString();
                    if (!"操作成功".equals(str)) {
                        return ResultUtils.getResults(Result.Status.ERROR, str);
                    }
                }
            } else {
                return ResultUtils.getResults(Result.Status.ERROR, "对不起，当前账号无审核权限");
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/noPass")
    public Result<String> noPass(@RequestBody CountryScoreParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            UserEntity userEntity = loginService.getUserByToken(param.tokenEntity);
            if ("agent".equals(userEntity.getUserPermissions())) {
                return ResultUtils.getResults(Result.Status.ERROR, "对不起，当前账号无审核权限");
            }
            int i = 0;
           // param.countryScoreEntity.setId(Long.valueOf(param.countryScoreEntity.getAlternateField2()));
          //  param.countryScoreEntity.setAlternateField2(null);
            i = countryScoreService.noPass(param.countryScoreEntity);
            if (i == 0) {
                return ResultUtils.getResults(Result.Status.ERROR, "操作失败，请联系管理员");
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }
}

class CountryScoreParam {
    public CountryIndicatorsFilter countryIndicatorsFilter;
    public CountryIndicatorsEntity countryIndicatorsEntity;
    public TokenEntity tokenEntity;
    public List<CountryScoreEntity> list;
    public String dateTime;
    public CountryScoreEntity countryScoreEntity;
    public CountryScoreFilter countryScoreFilter;
}
